package com.powernode.sqlinjection;

import com.powernode.commons.util.DbUtils;
import com.powernode.commons.util.ReleaseResourceUtils;

import java.sql.*;
import java.time.LocalDate;

//@SuppressWarnings("all")

/**
 * @Author Arrebol
 * @Date 2024/8/1 22:44
 * @Project jdbc
 * @Description：
 * 使用PreparedStatement完成新增操作
 */
public class JdbcTestPreparedStatement1 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DbUtils.getConnection();
            String insertSql = "insert into t_emp " +
                    "(id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
                    "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(insertSql);
            ps.setShort(1, (short) 100);
            ps.setString(2, "Arrebol");
            ps.setString(3, "makangji");
            ps.setString(4, "测试人员");
            ps.setShort(5, (short) 1);
            ps.setString(6, "James·Goslin.jpg");
            ps.setShort(7, (short) 4);
            ps.setDate(8, Date.valueOf(LocalDate.now()));
            ps.setShort(9, (short) 3);
            ps.setDate(10, Date.valueOf(LocalDate.now()));
            ps.setDate(11, Date.valueOf(LocalDate.now()));
            System.out.println("执行的SQL语句为："+insertSql);
            int count = ps.executeUpdate();
            System.out.println("插入了"+count+"条记录");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.close(null, ps, conn);
        }
    }
}
